<?php

namespace app\common\model;

use think\Db;

/**
 * 文章模型
 * Class Article
 * @package app\common\model
 */
class Video extends BaseModel
{
    protected $name = 'video';

    /**
     * 关联图片表
     * @return \think\model\relation\HasMany
     */
    public function image()
    {
        return $this->hasMany('VideoImage')->order(['id' => 'asc']);
    }

    /**
     * 关联分类表
     * @return \think\model\relation\BelongsTo
     */
    public function category()
    {
        //$module = self::getCalledModule() ?: 'common';
        return $this->BelongsTo("app\\common\\model\\video\\Category");
    }

    /**
     * 获取列表
     * @param $param
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function getList($param)
    {
        // 商品列表获取条件
        $params = array_merge([
            'status' => 10,         // 房间状态
            'category_id' => 0,     // 分类id
            'search' => '',         // 搜索关键词
            'sortType' => 'all',    // 排序类型
            'sortPrice' => false,   // 价格排序 高低
            'listRows' => 15,       // 每页数量
        ], $param);
        // 筛选条件
        $filter = [];
        $params['category_id'] > 0 && $filter['category_id'] = $params['category_id'];
        $params['status'] > 0 && $filter['video_status'] = $params['status'];
        !empty($params['search']) && $filter['video_name'] = ['like', '%' . trim($params['search']) . '%'];
        // 排序规则
        $sort = [];
        if ($params['sortType'] === 'all') {
            $sort = ['video_sort', 'video_id' => 'desc'];
        }
//        elseif ($params['sortType'] === 'price') {
//            $sort = $params['sortPrice'] ? ['room_price' => 'desc'] : ['room_price' => 'asc'];
//        }
        // 商品表名称
        $tableName = $this->getTable();
        // 执行查询
        $list = $this
            ->field(['*'])
            ->with(['category', 'image.file'])
            ->where('is_delete', '=', 0)
            ->where($filter)
            ->order($sort)
            ->paginate($params['listRows'], false, [
                'query' => \request()->request()
            ]);
        //var_dump($list);die();
        // 整理列表数据并返回
        return $this->setVideoListData($list, true);
        //return $list;
    }

    /**
     * 获取商品列表
     * @param $param
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function apiGetList($param)
    {
        // 商品列表获取条件
        $params = array_merge([
            'searchInput' => '',      //搜索关键词
            'sortType' => '',     //排序类型
            'roomPrice' => '',         //价格
            'position' => '',    //商圈id
            'city' => 42,   //当前城市id
            'peopleNum' => '',     //人数
            'startDate' => '',     //入驻日期
            'endtDate' => '',     //离开日期
            'listRows' => '',     // 每页数量
            'page' => '',     //第几页
        ], $param);
        // 筛选条件
        $filter = [];
        $params['status'] > 0 && $filter['video_status'] = $params['status'];
        !empty($params['searchInput']) && $filter['video_name'] = ['like', '%' . trim($params['search']) . '%'];
        // 排序规则
        $sort = [];
        if ($params['sortType'] === 'all') {
            $sort = ['video_sort', 'video_id' => 'desc'];
        }
//        elseif ($params['sortType'] === 'price') {
//            $sort = $params['sortPrice'] ? ['room_price' => 'desc'] : ['room_price' => 'asc'];
//        }
        // 商品表名称
        $tableName = $this->getTable();
        // 执行查询
        $list = $this
            ->field(['*'])
            ->with(['category', 'image.file'])
            ->where('is_delete', '=', 0)
            ->where($filter)
            ->order($sort)
            ->paginate($params['listRows'], false, [
                'query' => \request()->request()
            ]);
        //var_dump($list);die();
        // 整理列表数据并返回
        return $this->setApiRoomListData($list, true);
        //return $list;
    }

    /**
     * 房间详情
     * @param $article_id
     * @return null|static
     * @throws \think\exception\DbException
     */
    public static function detail($article_id)
    {
        return self::get($article_id, ['category', 'image.file']);
    }

    /**
     * 计费方式
     * @param $value
     * @return mixed
     */
    public function getVideoStatusAttr($value)
    {
        $status = [10 => '上架', 20 => '下架'];
        return ['text' => $status[$value], 'value' => $value];
    }


    /**
     * 设置房间展示的数据
     * @param $data
     * @param bool $isMultiple
     * @param callable $callback
     * @return mixed
     */
    protected function setVideoListData($data, $isMultiple = true, callable $callback = null)
    {
        if (!$isMultiple) $dataSource = [&$data]; else $dataSource = &$data;
        // 整理商品列表数据
        foreach ($dataSource as &$goods) {
            // 商品主图
            $goods['video_image'] = $goods['image'];
            $goods['image_count'] = count($goods['image']);
            // 回调函数
            is_callable($callback) && call_user_func($callback, $goods);
        }
        return $data;
    }

    /**
     * 设置api房间展示的数据
     * @param $data
     * @param bool $isMultiple
     * @param callable $callback
     * @return mixed
     */
    protected function setApiRoomListData($data, $isMultiple = true, callable $callback = null)
    {
        if (!$isMultiple) $dataSource = [&$data]; else $dataSource = &$data;
        // 整理商品列表数据
        foreach ($dataSource as &$goods) {
            // 商品主图
            $goods['room_image'] = $goods['image'];
            $goods['image_count'] = count($goods['image']);
            $goods['label_ids'] = explode(',', $goods['label_ids']);
            $a = [];
            //var_dump($goods['label_ids']);die();
            if (!empty($goods['label_ids'])) {
                $goods['ticket'] = Db::name('room_ticket')->where('category_id', 'in', $goods['label_ids'])->limit(6)->column('name');
            }
            // 回调函数
            is_callable($callback) && call_user_func($callback, $goods);
        }
        return $data;
    }

}
